<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle">
    <el-form
      :model="formData"
      ref="formRef"
      :rules="formRules"
      vloading="formLoading"
      label-width="auto"
    >
      <el-row>
        <el-col :span="24">
          <el-form-item label="岗位" prop="postIds">
            <el-select v-model="formData.postIds" multiple placeholder="请输入岗位">
              <el-option
                v-for="item in postList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="是否仅计算未成交客户" prop="isOnlyNoDeal">
            <el-checkbox v-model="formData.isOnlyNoDeal" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="客户总数" prop="customerCountLimit">
            <InputNumber
              v-model="formData.customerCountLimit"
              :min="1"
              placeholder="请输入客户总数"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="是否启用" prop="isEnable">
            <el-checkbox v-model="formData.isEnable" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button type="primary" :disabled="formLoading" @click="submitForm">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup name="PoolMaxLimitEdit" lang="ts">
import * as PostApi from '@/api/system/post/post'
import * as poolSettingApi from '@/api/setting/poolSetting'
const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const postList = ref() // 岗位列表
const dialogTitle = ref('')
const dialogVisible = ref(false)
const formRef = ref()
const formLoading = ref(false)
const formData = ref({
  id: undefined,
  postIds: [] as any[],
  limitPosts: [] as any[],
  isOnlyNoDeal: true,
  customerCountLimit: 1,
  isEnable: true
})
const formRules = reactive({
  postIds: [{ required: true, message: '岗位不能为空', trigger: 'blur' }]
})
/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 加载岗位列表
  postList.value = await PostApi.listAllSimple()
  if (id) {
    formLoading.value = true
    try {
      formData.value = await poolSettingApi.getMaxLimit(id)

      if (formData.value.limitPosts != undefined)
        formData.value.postIds = formData.value.limitPosts.map((rulepost) => rulepost.postId)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    postIds: [],
    limitPosts: [],
    isOnlyNoDeal: true,
    customerCountLimit: 1,
    isEnable: true
  }
  formRef.value?.resetFields()
}

const emit = defineEmits(['success'])
/** 提交表单 */
const submitForm = async () => {
  // 校验表单
  if (!formRef) return
  const valid = await formRef.value.validate()
  if (!valid) return
  // 提交请求
  formLoading.value = true
  try {
    if (formType.value === 'create') {
      await poolSettingApi.createMaxLimit(formData.value)
      message.success(t('common.createSuccess'))
    } else {
      await poolSettingApi.updateMaxLimit(formData.value)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}
</script>
